﻿using JX;
using OpenCvSharp;
using OpenCvSharp.Extensions;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace AITEST
{
    public partial class Form1 : Form
    {
        private JX.DetParam p =new JX.DetParam();
        public Form1()
        {
            p.modelWidth = 640;
            p.modelHeight = 640;
            p.confidenceThreshold = 0.8f;
            InitializeComponent();
        }
        private Mat img=null;
        private void button1_Click(object sender, EventArgs e)
        {
            _=JX.OnnxDetector.StartCameraAsync(pictureBox1,pictureBox2);
        }


        private void button3_Click(object sender, EventArgs e)
        {
            using (OpenFileDialog ofd = new OpenFileDialog())
            {
                ofd.Filter = "Image Files|*.jpg;*.jpeg;*.png;*.bmp";
                if (ofd.ShowDialog() == DialogResult.OK)
                {
                    try
                    {
                        img=JX.OnnxDetector.LoadImage(ofd.FileName);
                        pictureBox1.Image = img.ToBitmap();

                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show("处理失败: " + ex.Message);
                    }
                }
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (img!=null && !img.Empty())
            {
                List<DetResult> list = JX.OnnxDetector.Detect(img, p);
                Mat newimg=img.Clone();
                JX.OnnxDetector.DrawResults(newimg, list);
                pictureBox2.Image=newimg.ToBitmap();
                List<Mat> lp = JX.OnnxDetector.GetDetectionCrops(img, list);
                foreach (Mat p in lp)
                {
                    pictureBox3.Image = p.ToBitmap();
                }
                foreach (DetResult det in list) {
                    Mat p=JX.OnnxDetector.AdjustBarcodeOrientation(img, det);
                    pictureBox4.Image = p.ToBitmap();
                }
            }
        }
    }
}
